﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels.DialogViewModels/DataInitProcessBarViewModel
* 类       名: DataInitProcessBarViewModel
* 功      能:  TD
* 时      间:  2024-05-20 15:03:11
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using WaferSelectV3.Common;

namespace WaferSelectV3.ViewModels.DialogViewModels
{
    public class DataInitProcessBarViewModel : BindableBase, IDialogAware
    {
        private string? _title;

        public string? Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 上电初始化是否已经完成
        /// </summary>
        public bool DataInitIsFinish = false;

        private int _processValue;

        /// <summary>
        /// 进度条的值
        /// </summary>
        public int ProgressValue
        {
            get { return _processValue; }
            set
            {
                _processValue = value;
                ValuePercent = $"{value}%";
                RaisePropertyChanged();
            }
        }

        private string? _valuePercent;
        /// <summary>
        /// 显示的百分比
        /// </summary>
        public string? ValuePercent
        {
            get { return _valuePercent; }
            set
            {
                _valuePercent = value;
                RaisePropertyChanged();
            }
        }

        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public async void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters != null)
            {
                var serverArray = parameters.GetValue<SocketServer[]>("serverArray");

                // 窗口打开的时候,要不要传递参数过来
                await Task.Run(async () =>
                {
                    // 刷新界面即可
                    int loopCount = 0;
                    int loopCountMax = 50;
                    DataInitIsFinish = false;
                    while (!DataInitIsFinish)
                    {

                        if (++loopCount >= loopCountMax)
                        {
                            break;
                        }
                        Application.Current?.Dispatcher.Invoke(() =>
                        {
                            if (loopCount <= 10)
                            {
                                ProgressValue = loopCount * 9;
                            }
                            else
                            {
                                ProgressValue = 90 + (loopCount / 4);
                                ProgressValue = ProgressValue > 100 ? 100 : ProgressValue;
                            }
                            if (serverArray.All(x => x.ClearResponseIsOk))
                                DataInitIsFinish = true;
                        });
                        await Task.Delay(10);
                    }

                });
            }
            RequestClose?.Invoke(new DialogResult());
        }
    }
}
